﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

namespace Testify.Courses
{

   public partial class Courses : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
      }

      #region Load Drop Down Lists
      protected void loadCourseList(object sender, EventArgs e)
      {
         loadList(ddlCourseName);
      }
      protected void loadList(DropDownList sender)
      {
         sender.Items.Clear();
         sender.Items.Add(new ListItem("", "0"));
         sender.DataBind();
         if (sender.Items.Count == 1)
            sender.Enabled = false;
         else
            sender.Enabled = true;
      }
      #endregion

      #region TermTypePanel
      protected void toggleTermTypePanel(object sender, EventArgs e)
      {
         if (pnlTermType.Visible == false)
         {
            toggleSectionPanel(false);
            toggleCourseTypePanel(false);
            toggleCoursePanel(false);
            toggleTermPanel(false);

            bxAddTermTypeName.Text = String.Empty;
            lblErrorAddTermType.Text = String.Empty;

            lvTermType.DataBind();

            toggleTermTypePanel(true);
         }
         else
         {
            toggleTermTypePanel(false);
         }
      }
      private void toggleTermTypePanel(bool tog)
      {
         pnlTermType.Visible = tog;
      }
      protected void addTermType(object sender, EventArgs e)
      {
         lblErrorAddTermType.Text = String.Empty;
         if ((String.IsNullOrWhiteSpace(bxAddTermTypeName.Text)))
            lblErrorAddTermType.Text = "You must have a term type name!";
         else if (assertTermTypeNotExists(bxAddTermTypeName.Text))
            lblErrorAddTermType.Text = "This term type already exists!";
         else
         {
            addTermType();
            lblErrorAddTermType.Text = "Term Type added";
         }

      }
      private void addTermType()
      {
         OracleConnection cnnctTermType = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctTermType.Open();

         OracleCommand cmdAddTermType = new OracleCommand(@"
         BEGIN term_types.addTermType(
            p_AdminID      => :p_AdminID,
            p_TermTypeName => :p_TermTypeName);
         END;", cnnctTermType);

         cmdAddTermType.Parameters.Add("p_AdminID", Convert.ToDecimal(Session["UserID"]));
         cmdAddTermType.Parameters.Add("p_TermTypeName", bxAddTermTypeName.Text);
         cmdAddTermType.ExecuteNonQuery();
         cnnctTermType.Close();
         lvTermType.DataBind();
         bxAddTermTypeName.Text = String.Empty;
      }
      private bool assertTermTypeNotExists(string text)
      {
         try
         {
            OracleConnection assertTermTypeNotExists = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            assertTermTypeNotExists.Open();

            OracleCommand cmdAssertExists = new OracleCommand(@"
            BEGIN term_types.assertTermTypeNotExists(
               p_TermTypeName => :p_TermTypeName);
            END;", assertTermTypeNotExists);

            cmdAssertExists.Parameters.Add("p_TermTypeName", text);
            cmdAssertExists.ExecuteNonQuery();
            assertTermTypeNotExists.Close();
            return false;
         }
         catch
         {
            return true;
         }
      }
      protected void checkDeletedTermType(object sender, ListViewItemEventArgs e)
      {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
            if (((HiddenField)e.Item.FindControl("TermTypeMaint")).Value == "D")
            {
               e.Item.FindControl("btnTermTypeEdit").Visible = false;
               e.Item.FindControl("btnTermTypeDelete").Visible = false;
               e.Item.FindControl("btnTermTypeReactivate").Visible = true;
               ((HtmlTableCell)e.Item.FindControl("termTypeName")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termTypeGetBig")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termTypeGetBig")).ColSpan = 2;
               e.Item.FindControl("termTypeHideMe").Visible = false;
            }
         }
      }
      protected void reactivateTermType(object sender, EventArgs e)
      {
         OracleConnection cnnctReactivateTermType = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctReactivateTermType.Open();

         OracleCommand cmdReactivateTermType = new OracleCommand(@"
            BEGIN 
               UPDATE term_type
                  SET term_type_maint = 'C'
                WHERE term_type_id = :p_TermTypeID;
            END;", cnnctReactivateTermType);

         int v_TermTypeID;
         int.TryParse(((LinkButton)sender).CommandArgument, out v_TermTypeID);
         cmdReactivateTermType.Parameters.Add("p_TermTypeID", v_TermTypeID);
         cmdReactivateTermType.ExecuteNonQuery();
         cnnctReactivateTermType.Close();
         lvTermType.DataBind();
         logData(30, "", "", v_TermTypeID, "Reactivated");
      }
      protected void termTypeUpdating(object sender, ListViewUpdateEventArgs e)
      {
         lblErrorAddTermType.Text = String.Empty;
         string termName;
         bool valid = true;
         e.Cancel = true;
         
            termName = e.NewValues["term_type_name"].ToString();

            if ((String.IsNullOrWhiteSpace(termName)))
            {
               lblErrorAddTermType.Text = "You must have a term type name!";
               valid = false;
            }
            if (String.Compare(e.OldValues["term_type_name"].ToString(),e.NewValues["term_type_name"].ToString()) != 0)
            {
               if (assertTermTypeNotExists(termName))
               {
                  lblErrorAddTermType.Text += "<br/>This term type already exists!";
                  valid = false;
               }
            }
            if (valid)
            {
               e.Cancel = false;
               lblErrorAddTermType.Text = "Term Type updated.";
            }
         
      }
      #endregion

      #region TermPanel
      protected void toggleTermPanel(object sender, EventArgs e)
      {
         loadList(ddlTermType);

         if (pnlTerm.Visible == false)
         {
            toggleSectionPanel(false);
            toggleCourseTypePanel(false);
            toggleCoursePanel(false);
            toggleTermTypePanel(false);

            bxStartDate.Text = String.Empty;
            bxEndDate.Text = String.Empty;
            lblErrorAddTerm.Text = String.Empty;

            lvTerm.DataBind();

            toggleTermPanel(true);
         }
         else
         {
            toggleTermPanel(false);
         }
      }
      protected void toggleTermPanel(bool tog)
      {
         pnlTerm.Visible = tog;
      }
      protected void addTerm(object sender, EventArgs e)
      {
         lblErrorAddTerm.Text = String.Empty;
         DateTime startDate,
                  endDate;
         int termTypeID;
         int.TryParse(ddlTermType.SelectedValue, out termTypeID);
         if (DateTime.TryParse(bxStartDate.Text, out startDate) && DateTime.TryParse(bxEndDate.Text, out endDate))
         {
            if (assertTermNotExists(startDate, termTypeID))
            {
               if (startDate < DateTime.Today)
               {
                  lblErrorAddTerm.Text = "You cannot select a day earlier than today!";
               }
               else if (startDate > endDate)
               {
                  lblErrorAddTerm.Text = "Your term cannot start after it has finished!";
               }
               else if (startDate < endDate.AddYears(-1))
               {
                  lblErrorAddTerm.Text = "Your term cannot be more than a year long!";
               }
               else
               {
                  addTerm();
                  lblErrorAddTerm.Text = "Term added.";
               }
            }
            else
            {
               lblErrorAddTerm.Text = "This type of term already exists for this year";
            }
         }
         else
         {
            if (!DateTime.TryParse(bxStartDate.Text, out startDate))
               lblErrorAddTerm.Text = "The start date must be in the following format: (MM/DD/YYY)<br />";
            if (!DateTime.TryParse(bxEndDate.Text, out endDate))
               lblErrorAddTerm.Text += "The end date must be in the following format: (MM/DD/YYY)";
         }
      }
      private void addTerm()
      {
         OracleConnection cnnctAddTerm = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctAddTerm.Open();

         OracleCommand cmdAddTerm = new OracleCommand(@"
               BEGIN terms.addTerm(
                  p_AdminID       => :p_AdminID,
                  p_TermTypeID    => :p_TermTypeID,
                  p_TermStartDate => to_date(:p_TermStartDate, 'MM/DD/YYYY'),
                  p_TermEndDate   => to_date(:p_TermEndDate, 'MM/DD/YYYY'));
               END;", cnnctAddTerm);

         cmdAddTerm.Parameters.Add("p_AdminID", Convert.ToDecimal(Session["UserID"]));
         cmdAddTerm.Parameters.Add("p_TermTypeID", ddlTermType.SelectedValue);
         cmdAddTerm.Parameters.Add("p_TermStartDate", bxStartDate.Text);
         cmdAddTerm.Parameters.Add("p_TermEndDate", bxEndDate.Text);
         cmdAddTerm.ExecuteNonQuery();
         cnnctAddTerm.Close();
         lvTerm.DataBind();
      }
      private bool assertTermNotExists(DateTime startDate, int termTypeID)
      {
         try
         {
            OracleConnection cnnctAssertTermNotExists = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            cnnctAssertTermNotExists.Open();

            OracleCommand cmdAssertTermNotExist = new OracleCommand(@"
            BEGIN terms.assertTermNotExists(
               p_TermStartDate => :p_TermStartDate,
               p_TermTypeID    => :p_TermTypeID);
            END;", cnnctAssertTermNotExists);

            cmdAssertTermNotExist.Parameters.Add("p_TermStartDate", startDate);
            cmdAssertTermNotExist.Parameters.Add("p_TermTypeID", termTypeID);
            cmdAssertTermNotExist.ExecuteNonQuery();
            cnnctAssertTermNotExists.Close();
            return true;
         }
         catch
         {
            return false;
         }
      }
      protected void checkTerm(object sender, ListViewItemEventArgs e)
      {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
            DateTime date;
            try
            {
               DateTime.TryParse(((Label)e.Item.FindControl("lblTermEndDate")).Text, out date);
            }
            catch
            {
               date = DateTime.Now;
            }
            if (date < DateTime.Now)
            {
               e.Item.FindControl("btnTermEdit").Visible = false;
               e.Item.FindControl("btnTermDelete").Visible = false;
               ((HtmlTableCell)e.Item.FindControl("termGetBig")).ColSpan = 2;
               e.Item.FindControl("termHideMe").Visible = false;
            }
            else if (lvTerm.EditIndex != e.Item.DataItemIndex && ((LinkButton)e.Item.FindControl("btnTermReactivate")).CommandArgument == getCurrentTermID())
            {

                e.Item.FindControl("btnTermEdit").Visible = true;
                e.Item.FindControl("btnTermDelete").Visible = false;
                ((HtmlTableCell)e.Item.FindControl("termGetBig")).ColSpan = 1;
                e.Item.FindControl("termHideMe").Visible = true;
                e.Item.FindControl("lblCurrent").Visible = true;
            }
            else if (((HiddenField)e.Item.FindControl("TermMaint")).Value == "D")
            {
               e.Item.FindControl("btnTermEdit").Visible = false;
               e.Item.FindControl("btnTermDelete").Visible = false;
               e.Item.FindControl("btnTermReactivate").Visible = true;
               ((HtmlTableCell)e.Item.FindControl("termTermTypeName")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termStartDate")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termEndDate")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termGetBig")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("termGetBig")).ColSpan = 2;
               e.Item.FindControl("termHideMe").Visible = false;
            }
         }
      }
      protected string getCurrentTermID()
      {
          OracleCommand cmdGetStudentName = new OracleCommand(@"
SELECT current_term_id
  FROM environment",
                new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString));

          cmdGetStudentName.Connection.Open();
          string currentTermID = Convert.ToString(cmdGetStudentName.ExecuteScalar());
          cmdGetStudentName.Connection.Close();

          return currentTermID;
      }
      protected void reactivateTerm(object sender, EventArgs e)
      {
         OracleConnection cnnctReactivateTerm = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctReactivateTerm.Open();

         OracleCommand cmdReactivateTerm = new OracleCommand(@"
            BEGIN 
               UPDATE term
                  SET term_maint = 'C'
                WHERE term_id = :p_TermID;
            END;", cnnctReactivateTerm);

         int v_TermID;
         int.TryParse(((LinkButton)sender).CommandArgument, out v_TermID);
         cmdReactivateTerm.Parameters.Add("p_TermID", v_TermID);
         cmdReactivateTerm.ExecuteNonQuery();
         cnnctReactivateTerm.Close();
         lvTerm.DataBind();
         logData(17, "", "", v_TermID, "Reactivated");
      }
      protected void termUpdating(object sender, ListViewUpdateEventArgs e)
      {
         lblErrorAddTerm.Text = String.Empty;
         DateTime oldStart,
                  startDate,
                  endDate;
         bool valid = true;
         int termTypeID;
         int.TryParse(e.NewValues["term_type_id"].ToString(), out termTypeID);
         DateTime.TryParse(e.OldValues["term_start"].ToString(), out oldStart);

         
         if (!DateTime.TryParse(e.NewValues["term_start"].ToString(), out startDate))
         {
            lblErrorAddTerm.Text = "The start date must be in the following format: (MM/DD/YYYY)<br />";
            valid = false;
         }
         if (!DateTime.TryParse(e.NewValues["term_end"].ToString(), out endDate))
         {
            lblErrorAddTerm.Text += "<br/>The end date must be in the following format: (MM/DD/YYYY)";
            valid = false;
         }
         if (valid) // doesn't go here if input dates are bad
         {

            if (oldStart.Year != startDate.Year || String.Compare(e.OldValues["term_type_id"].ToString(), e.NewValues["term_type_id"].ToString()) != 0)
            {
               if (!assertTermNotExists(startDate, termTypeID))
               {
                  lblErrorAddTerm.Text += "<br/>This type of term already exists for this year";
                  valid = false;
               }
            }
            if (startDate > endDate)
            {
               lblErrorAddTerm.Text += "<br/>Your term cannot start after it has finished! Please change the end date, or the start date";
               valid = false;
            }
            if (startDate < endDate.AddYears(-1))
            {
               lblErrorAddTerm.Text += "<br/>Your term cannot be more than a year long!";
               valid = false;
            }
            if (valid)
            {
               if (endDate < DateTime.Today)
                  lblErrorAddTerm.Text = "You have set the term to have already ended. You can no longer use this term!";
               if (oldStart < DateTime.Today)
                  lblErrorAddTerm.Text += "You cannot change the start day after the term starts, the old start date has been kept.";
               valid = true;
               lblErrorAddTerm.Text += "<br/>Term updated.";
            }
            else
               e.Cancel = true;
         }
      }
      #endregion

      #region CourseType (Field)
      protected void toggleCourseTypePanel(object sender, EventArgs e)
      {
         if (pnlCourseType.Visible == false)
         {
            toggleSectionPanel(false);
            toggleCoursePanel(false);
            toggleTermPanel(false);
            toggleTermTypePanel(false);

            bxCoursePrefix.Text = String.Empty;
            bxCourseTypeName.Text = String.Empty;
            lblErrorAddCourseType.Text = String.Empty;

            lvCourseType.DataBind();

            toggleCourseTypePanel(true);
         }
         else
         {
            toggleCourseTypePanel(false);
         }
      }
      private void toggleCourseTypePanel(bool tog)
      {
         pnlCourseType.Visible = tog;
      }
      protected void addCourseType(object sender, EventArgs e)
      {
         lblErrorAddCourseType.Text = String.Empty;
         if (bxCourseTypeName.Text.Count() < 2)
         {
            lblErrorAddCourseType.Text = "The prefix must have 2 letters";
         }
         else if (assertCourseTypeNotExist(bxCoursePrefix.Text.ToString()))
         {
            lblErrorAddCourseType.Text = "A Field already exists with that Prefix (" + bxCoursePrefix.Text.ToUpper() + ")";
         }
         else if (bxCourseTypeName.Text == String.Empty)
         {
            lblErrorAddCourseType.Text = "The Field must have a name";
         }
         else
         {
            addCourseType();
         }
      }
      private bool assertCourseTypeNotExist(string prefix)
      {
         try
         {
            OracleConnection cnnctAssertCourseTypeNotExists = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            cnnctAssertCourseTypeNotExists.Open();

            OracleCommand cmdAssertCourseTypeNotExist = new OracleCommand(@"
            BEGIN course_types.assertCourseTypeNotExists(
               p_CourseTypePrefix => :p_CourseTypePrefix);
            END;", cnnctAssertCourseTypeNotExists);

            cmdAssertCourseTypeNotExist.Parameters.Add("p_CourseTypePrefix", prefix.ToUpper());
            cmdAssertCourseTypeNotExist.ExecuteNonQuery();
            cnnctAssertCourseTypeNotExists.Close();
            return false;
         }
         catch
         {
            return true;
         }
      }
      protected void addCourseType()
      {
         OracleConnection cnnctAddCourseType = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctAddCourseType.Open();

         OracleCommand cmdAddCourseType = new OracleCommand(@"
         BEGIN course_types.addCourseType(
            p_AdminID          => :p_AdminID,
            p_CourseTypePrefix => :p_CourseTypePrefix,
            p_CourseTypeName   => :p_CourseTypeName);
         END;", cnnctAddCourseType);

         cmdAddCourseType.Parameters.Add("p_AdminID", Convert.ToDecimal(Session["UserID"]));
         cmdAddCourseType.Parameters.Add("p_CourseTypePrefix", bxCoursePrefix.Text);
         cmdAddCourseType.Parameters.Add("p_CourseTypeName", bxCourseTypeName.Text);
         cmdAddCourseType.ExecuteNonQuery();
         cnnctAddCourseType.Close();

         lvCourseType.DataBind();

         loadList(ddlAddCourseCourseType);
         loadList(ddlCourseType);
      }
      protected void checkCourseType(object sender, ListViewItemEventArgs e)
      {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
            if (((HiddenField)e.Item.FindControl("hdCourseTypeMaint")).Value == 'D'.ToString())
            {
               e.Item.FindControl("btnCourseTypeEdit").Visible = false;
               e.Item.FindControl("btnCourseTypeDelete").Visible = false;
               e.Item.FindControl("btnCourseTypeReactivate").Visible = true;
               ((HtmlTableCell)e.Item.FindControl("courseTypePrefix")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseTypeName")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseTypeGetBig")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseTypeGetBig")).ColSpan = 2;
               e.Item.FindControl("courseTypeHideMe").Visible = false;
            }
         }
      }
      protected void reactivateCourseType(object sender, EventArgs e)
      {
         OracleConnection cnnctReactivateCourseType = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctReactivateCourseType.Open();

         OracleCommand cmdReactivateCourseType = new OracleCommand(@"
            BEGIN 
               UPDATE course_type
                  SET course_type_maint = 'C'
                WHERE course_type_id = :p_CourseTypeID;
            END;", cnnctReactivateCourseType);

         int v_CourseTypeID;
         int.TryParse(((LinkButton)sender).CommandArgument, out v_CourseTypeID);
         cmdReactivateCourseType.Parameters.Add("p_CourseTypeID", v_CourseTypeID);
         cmdReactivateCourseType.ExecuteNonQuery();
         cnnctReactivateCourseType.Close();
         lvCourseType.DataBind();
         logData(20, "", "", v_CourseTypeID, "Reactivated");
      }
      protected void courseTypeUpdating(object sender, ListViewUpdateEventArgs e)
      {
         e.Cancel = true;
         lblErrorAddCourseType.Text = String.Empty;
         if ((String.Compare(e.NewValues["course_type_prefix"].ToString(), e.OldValues["course_type_prefix"].ToString()) != 0) && assertCourseTypeNotExist(e.NewValues["course_type_prefix"].ToString()))
         {
            lblErrorAddCourseType.Text = "This prefix is already taken!";
         }
         else if (String.IsNullOrWhiteSpace(e.NewValues["course_type_name"].ToString()))
         {
            lblErrorAddCourseType.Text = "The name cannot be blank!";
         }
         else
         {
            lblErrorAddCourseType.Text = "Field Updated";
            e.Cancel = false;
         }
      }
      #endregion

      #region CoursePanel
      protected void toggleCoursePanel(object sender, EventArgs e)
      {
         if (pnlCourse.Visible == false)
         {
            toggleSectionPanel(false);
            toggleTermPanel(false);
            toggleCourseTypePanel(false);
            toggleTermTypePanel(false);

            loadList(ddlAddCourseCourseType);
            bxCourseNumber.Text = String.Empty;
            bxCourseName.Text = String.Empty;
            lblErrorAddClass.Text = String.Empty;



            toggleCoursePanel(true);
         }
         else
         {
            toggleCoursePanel(false);
         }
      }
      private void toggleCoursePanel(bool tog)
      {
         pnlCourse.Visible = tog;
      }
      protected void addCourse(object sender, EventArgs e)
      {
         lblErrorAddClass.Text = String.Empty;
         int courseTypeID;
         int.TryParse(ddlAddCourseCourseType.SelectedValue, out courseTypeID);
         if (ddlAddCourseCourseType.SelectedValue == 0.ToString())
         {
            lblErrorAddClass.Text = "Please select a Field!";
         }
         else if (bxCourseName.Text == String.Empty)
         {
            lblErrorAddClass.Text = "Your course needs a name!";
         }
         else if (assertCourseNotExists(courseTypeID, getInt(bxCourseNumber)))
         {
            lblErrorAddClass.Text = "A course already exists with that Field and Number!";
         }
         else if (bxCourseNumber.Text.Length < 3)
         {
            lblErrorAddClass.Text = "The course number must be three digits long!";
         }
         else
         {
            addCourse();
         }
      }
      private bool assertCourseNotExists(int CourseTypeID, int CourseNumber)
      {
         try
         {
            OracleConnection assertCrsNotExists = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            assertCrsNotExists.Open();

            OracleCommand cmdAssertExists = new OracleCommand(@"
            BEGIN courses.assertCourseNotExists(
               p_CourseTypeID   => :p_CourseTypeID,
               p_CourseNumber   => :p_CourseNumber);
            END;", assertCrsNotExists);

            cmdAssertExists.Parameters.Add("p_CourseTypeID", CourseTypeID);
            cmdAssertExists.Parameters.Add("p_CourseNumber", CourseNumber);
            cmdAssertExists.ExecuteNonQuery();
            assertCrsNotExists.Close();
            return false;
         }
         catch
         {
            return true;
         }
      }
      private void addCourse()
      {
         OracleConnection cnnctAddCourse = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctAddCourse.Open();

         OracleCommand cmdAddCourse = new OracleCommand(@"
         BEGIN courses.addCourse(
            p_AdminID        => :p_AdminID,
            p_CourseName     => :p_CourseName,
            p_CourseTypeID   => :p_CourseTypeID,
            p_CourseNumber   => :p_CourseNumber);
         END;", cnnctAddCourse);

         cmdAddCourse.Parameters.Add("p_AdminID", Convert.ToDecimal(Session["UserID"]));
         cmdAddCourse.Parameters.Add("p_CourseName", bxCourseName.Text);
         cmdAddCourse.Parameters.Add("p_CourseTypeID", ddlAddCourseCourseType.SelectedValue);
         cmdAddCourse.Parameters.Add("p_CourseNumber", getInt(bxCourseNumber));
         cmdAddCourse.ExecuteNonQuery();
         cnnctAddCourse.Close();

         lvCourse.DataBind();

         loadList(ddlCourseType);
      }
      protected void checkCourse(object sender, ListViewItemEventArgs e)
      {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
            if (((HiddenField)e.Item.FindControl("hdCourseMaint")).Value == 'D'.ToString())
            {
               e.Item.FindControl("btnCourseEdit").Visible = false;
               e.Item.FindControl("btnCourseDelete").Visible = false;
               e.Item.FindControl("btnCourseReactivate").Visible = true;
               ((HtmlTableCell)e.Item.FindControl("courseTypePrefix")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseNumber")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseName")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseGetBig")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("courseGetBig")).ColSpan = 2;
               e.Item.FindControl("courseHideMe").Visible = false;
            }
         }
      }
      protected void reactivateCourse(object sender, EventArgs e)
      {
         OracleConnection cnnctReactivateCourse = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctReactivateCourse.Open();

         OracleCommand cmdReactivateCourse = new OracleCommand(@"
            BEGIN 
               UPDATE course
                  SET course_maint = 'C'
                WHERE course_id = :p_CourseID;
            END;", cnnctReactivateCourse);

         int v_CourseID;
         int.TryParse(((LinkButton)sender).CommandArgument, out v_CourseID);
         cmdReactivateCourse.Parameters.Add("p_CourseID", v_CourseID);
         cmdReactivateCourse.ExecuteNonQuery();
         cnnctReactivateCourse.Close();
         lvCourse.DataBind();
         logData(13, "", "", v_CourseID, "Reactivated");
      }
      protected void courseUpdating(object sender, ListViewUpdateEventArgs e)
      {
         lblErrorAddClass.Text = String.Empty;
         int courseTypeID;
         int courseNumber;
         int.TryParse(e.NewValues["course_type_id"].ToString(), out courseTypeID);
         bool valid;

         if (!int.TryParse(e.NewValues["course_number"].ToString(), out courseNumber))
         {
            valid = false;
            lblErrorAddClass.Text = "The Course Numbe is invalid!";
         }
         else if (String.IsNullOrWhiteSpace(e.NewValues["course_name"].ToString()))
         {
            valid = false;
            lblErrorAddClass.Text = "Your course needs a name!";
         }
         else if (String.Compare(e.OldValues["course_number"].ToString(), e.NewValues["course_number"].ToString()) != 0 || String.Compare(e.OldValues["course_type_id"].ToString(), e.NewValues["course_type_id"].ToString()) != 0)
         {
            if (assertCourseNotExists(courseTypeID, courseNumber))
            {
               valid = false;
               lblErrorAddClass.Text = "A course already exists with that Field and Number!";
            }
            else
               valid = true;
         }
         else if (e.NewValues["course_number"].ToString().Length < 3)
         {
            valid = false;
            lblErrorAddClass.Text = "The course number must be three digits long!";
         }
         else
         {
            valid = true;
         }
         if (valid)
         {
            e.Cancel = false;
            lblErrorAddClass.Text = "Course Updated.";
         }
         else
            e.Cancel = true;
      }
      #endregion

      #region SectionPanel
      protected void toggleSectionPanel(object sender, EventArgs e)
      {
         if (pnlSection.Visible == false)
         {
            toggleCoursePanel(false);
            toggleCourseTypePanel(false);
            toggleTermPanel(false);
            toggleTermTypePanel(false);

            lblErrorAddSection.Text = String.Empty;

            loadList(ddlTerm);
            loadList(ddlCourseType);
            loadList(ddlCourseName);
            loadList(ddlTeacher);

            lvSection.DataBind();

            toggleSectionPanel(true);
         }
         else
         {
            toggleSectionPanel(false);
         }
      }
      private void toggleSectionPanel(bool tog)
      {
         pnlSection.Visible = tog;
      }
      protected void addSection(object sender, EventArgs e)
      {
         lblErrorAddSection.Text = String.Empty;
         if (ddlTerm.SelectedIndex == 0)
            lblErrorAddSection.Text = "Please Choose a Term";
         else if (ddlCourseName.SelectedIndex == 0)
            lblErrorAddSection.Text = "Please Choose a Course";
         else if (ddlTeacher.SelectedIndex == 0)
            lblErrorAddSection.Text = "Please Choose a Teacher";
         else
            addClassSection();
      }
      private void addClassSection()
      {
         OracleConnection cnnctAddSection = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctAddSection.Open();

         OracleCommand cmdAddSection = new OracleCommand(@"
         BEGIN sections.addClassSection(
            p_AdminID   => :p_AdminID,
            p_CourseID  => :p_CourseID,
            p_TermID    => :p_TermID,
            p_TeacherID => :p_TeacherID);
         END;", cnnctAddSection);

         cmdAddSection.Parameters.Add("p_AdminID", Convert.ToDecimal(Session["UserID"]));
         cmdAddSection.Parameters.Add("p_CourseID", ddlCourseName.SelectedValue);
         cmdAddSection.Parameters.Add("p_TermID", ddlTerm.SelectedValue);
         cmdAddSection.Parameters.Add("p_TeacherID", ddlTeacher.SelectedValue);
         cmdAddSection.ExecuteNonQuery();
         cnnctAddSection.Close();
         lvSection.DataBind();
      }
      protected void checkSection(object sender, ListViewItemEventArgs e)
      {
         if (e.Item.ItemType == ListViewItemType.DataItem)
         {
            DateTime date;
            try
            {
               DateTime.TryParse(((HiddenField)e.Item.FindControl("hdTermEnd")).Value, out date);
            }
            catch
            {
               date = DateTime.Now;
            }
            if (date < DateTime.Now)
            {
               e.Item.FindControl("btnSectionEdit").Visible = false;
               e.Item.FindControl("btnSectionDelete").Visible = false;
               ((HtmlTableCell)e.Item.FindControl("sectionGetBig")).ColSpan = 2;
               e.Item.FindControl("sectionHideMe").Visible = false;
            }
            else if (((HiddenField)e.Item.FindControl("hdSectionMaint")).Value == 'D'.ToString())
            {
               e.Item.FindControl("btnSectionEdit").Visible = false;
               e.Item.FindControl("btnSectionDelete").Visible = false;
               e.Item.FindControl("btnSectionReactivate").Visible = true;
               ((HtmlTableCell)e.Item.FindControl("sectionTerm")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("sectionCourseFancy")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("sectionTeacher")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("sectionGetBig")).BgColor = "#555555";
               ((HtmlTableCell)e.Item.FindControl("sectionGetBig")).ColSpan = 2;
               e.Item.FindControl("sectionHideMe").Visible = false;
            }

         }
      }
      protected void reactivateSection(object sender, EventArgs e)
      {
         OracleConnection cnnctReactivateSection = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         cnnctReactivateSection.Open();

         OracleCommand cmdReactivateSection = new OracleCommand(@"
            BEGIN 
               UPDATE section
                  SET section_maint = 'C'
                WHERE section_id = :p_SectionID;
            END;", cnnctReactivateSection);

         int v_SectionID;
         int.TryParse(((LinkButton)sender).CommandArgument, out v_SectionID);
         cmdReactivateSection.Parameters.Add("p_SectionID", v_SectionID);
         cmdReactivateSection.ExecuteNonQuery();
         cnnctReactivateSection.Close();
         lvSection.DataBind();
         logData(26, "", "", v_SectionID, "Reactivated");
      }
      protected void sectionUpdating(object sender, ListViewUpdateEventArgs e)
      {
         lblErrorAddSection.Text = "Section Updated";
      }
      #endregion

      #region Extra functions
      private int getInt(TextBox sender)
      {
         int checkerInt;

         int.TryParse(sender.Text, out checkerInt);

         return checkerInt;
      }
      private void logData(int actionID, string IP, string HostName, int recipientID, string comment)
      {
         OracleConnection logData = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
         logData.Open();

         OracleCommand cmdLogData = new OracleCommand(@"
            BEGIN logs.logAction(
               p_ActionID          => :p_ActionID,
               p_UserID            => :p_UserID,
               p_IP                => :p_IP,
               p_HostName          => :p_HostName,
               p_ActionRecipientID => :p_ActionRecipientID,
               p_LogData           => :p_LogData);
            END;", logData);
         cmdLogData.Parameters.Add("p_ActionID", actionID);
         cmdLogData.Parameters.Add("p_UserID", Convert.ToDecimal(Session["UserID"]));
         cmdLogData.Parameters.Add("p_IP", IP);
         cmdLogData.Parameters.Add("p_HostName", HostName);
         cmdLogData.Parameters.Add("p_ActionRecipientID", recipientID);
         cmdLogData.Parameters.Add("p_LogData", comment);
         cmdLogData.ExecuteNonQuery();
         logData.Close();
      }
      protected void setStart(object sender, EventArgs e)
      {
         ((CalendarExtender)sender).StartDate = DateTime.Now;
      }
      #endregion
   }
}